<template>
  <div class="changePermission">
    <el-row :gutter="30">
      <el-col :span="12">
        <el-row>

        </el-row>
        <el-row class="mt-10">
          <el-form ref="form" :model="form" label-width="100px" :rules="rules" autocomplete="off">

            <el-form-item label="账号" prop="uid">
              <el-input v-model="form.uid" placeholder="管理员账号" autocomplete="off"></el-input>
            </el-form-item>

            <el-form-item label="权限等级" prop="rid">

              <el-select v-model="form.rid" placeholder="请选择权限等级" style="width: 100%">
                <el-option
                  v-for="item in rid"
                  :key="item.rid"
                  :label="item.nameZn"
                  :value="item.rid">
                </el-option>

              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="submitForm('form')">修改</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-row>
      </el-col>
    </el-row>


  </div>
</template>

<script>
  export default {
    name: "addadmin",
    data() {
      var checkusername = (rule, value, callback) => {
        var self = this;
        if (value === '') {
          callback(new Error('请输入用户名'));
        } else {

          this.$http.get(this.URL.api + "user/findbyusername/" + value).then(res => {

            if (res.data.flag) {
              self.uid = res.data.data
              console.log(self.uid)
              callback();
            } else {
              self.form.uid = ''
              self.form.rid = ''
              callback(new Error('用户不存在'));

            }

          })


        }
      };
      return {
        uid: "",
        rid: [],
        form: {
          uid: '',
          rid: '',
        },
        rules: {
          uid: [
            {required: true, validator: checkusername, trigger: 'blur'},
          ],
          rid: [
            {required: true, message: '请选择角色', trigger: 'change'}
          ]


        }
      }
    },
    methods: {

      submitForm(formName) {
        var self = this;
        this.$refs[formName].validate((valid) => {
          if (valid) {

            this.$http.put(this.URL.api + "user/updaterole", {uid:this.uid,rid:this.form.rid}).then(res => {

              console.log(res)

            })

          } else {

            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },

      onSubmit() {
        console.log('submit!');
      },
    },
    mounted: function () {
      this.$http.get(this.URL.api + "user/queryAllRole").then(res => {
        this.rid = res.data.data
        console.log(res.data)
      })
    }

  }
</script>

<style scoped>

</style>
